<template>
  <el-submenu v-if="menu.children && menu.children.length >= 1 && menu.showFlag!=0" :index="menu.id"
    :popper-append-to-body="false">
    <template slot="title">
      <svg class="icon-svg aui-sidebar__menu-icon" aria-hidden="true">
        <use :xlink:href="`#${menu.icon}`"></use>
      </svg>
      <span>{{ menu.name }}</span>
    </template>
    <sub-menu v-for="item in menu.children" :key="item.id" :menu="item"></sub-menu>
  </el-submenu>
  <el-menu-item v-else-if="menu.showFlag!=0" :index="menu.id" @click="gotoRouteHandle(menu.id)">
    <svg class="icon-svg aui-sidebar__menu-icon" aria-hidden="true">
      <use :xlink:href="`#${menu.icon}`"></use>
    </svg>
    <span>{{ menu.name }}</span>
  </el-menu-item>
</template>
<style>
.aui-sidebar__menu .el-menu-item,
.aui-sidebar__menu .el-submenu__title {
  height: 40px;
  font-size: 13px;
  line-height: 40px;
}

.el-menu-item {
  font-size: 13px;
  text-align: left;
}

.el-submenu__title {
  font-size: 13px;
}
</style>
<script>
import SubMenu from './main-sidebar-sub-menu'
import { getRouteByMenuId } from './layout/common/layout'
export default {
  name: 'sub-menu',
  props: {
    menu: {
      type: Object,
      required: true
    }
  },
  components: {
    SubMenu
  },
  methods: {
    // 通过menuId与动态(菜单)路由进行匹配跳转至指定路由
    gotoRouteHandle (menuId) {
      let route = getRouteByMenuId(menuId)
      if (route) {
        const { iframeURL } = route.meta
        const { _blank } = route.params
        if (typeof (_blank) !== 'undefined' && _blank.toString() !== '0' && _blank.toString() !== 'false') {
          window.open(iframeURL, '_blank')
          this.$userBehavior.sendUserBehavior(iframeURL, route.meta.menuId, 0, 'openOtherMenu', route.meta.title)
        } else {
          this.$router.push({ name: route.name, params: route.params })
          this.$userBehavior.sendUserBehavior(route.path, route.meta.menuId, 0, 'openMenu', route.meta.title)
        }
      }
    }
  }
}
</script>
